package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.project.tool.swagger.UserController.SignUtil;
import okhttp3.HttpUrl;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.http.MediaType;

public class HttpUtil {
    public static JSONObject doPost(String url, JSONObject param, String sign) {
        HttpPost httpPost = null;
        String result = null;
        HttpClient client = new DefaultHttpClient();
        try {
            httpPost = new HttpPost(url);
            if (param != null) {
                StringEntity se = new StringEntity(param.toString(), "utf-8");
                httpPost.setEntity(se); // post方法中，加入json数据
                httpPost.setHeader("Content-Type", "application/json");
                httpPost.addHeader("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                if (StringUtils.isNotEmpty(sign)) {
                    httpPost.setHeader("Authorization", "WECHATPAY2-SHA256-RSA2048 " + sign);
                }
            }
            HttpResponse response = client.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, "utf-8");
                }

            }
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new BaseException("请求错误!");
        } finally {
            httpPost.releaseConnection();
        }
        System.out.println(result.toString());
        return JSONObject.parseObject(result);
    }

    public static JSONObject doPosts(String url) {
        HttpPost httpPost = null;
        String result = null;
        HttpClient client = new DefaultHttpClient();
        try {
            httpPost = new HttpPost(url);
            httpPost.setHeader("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
            httpPost.addHeader("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
            HttpResponse response = client.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, "utf-8");
                }

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            httpPost.releaseConnection();
        }
        return JSONObject.parseObject(result);
    }

/*    public static void main(String[] args) {
        String key="LD166T";
        String bankUserSign="oHLDewHNXVvLDlLg6yPUcfDx6EiM";
//        long time=System.currentTimeMillis()/1000;
        long time=1611293220l;
        //测试环境为0,正式环境为9
        String proof="0";
        JSONArray prize=new JSONArray();
        JSONObject obj=new JSONObject();
        obj.put("id","0");
        obj.put("name","1");
        prize.add(obj);
        String value=bankUserSign+proof+time+key;
        System.out.println(value);
        String token = new String(Md5Util.md5(value));

        JSONObject data=new JSONObject();
        data.put("bank_user_sign",bankUserSign);
        data.put("proof",proof);
        data.put("time",time);
        data.put("token",token);
        data.put("prize",prize.toJSONString());
        System.out.println(data.toJSONString());
        doPost("https://csp-brdc.dccnet.com.cn/weChatActivity/activity/qualificationQuery",data);
    }*/
 /*   public static void main(String[] args) {
        String key="LD166T";
        String bankUserSign="oHLDewHNXVvLDlLg6yPUcfDx6EiM";
//        long time=System.currentTimeMillis()/1000;
        long time=1611293220l;
        String proof="622e76ce-3b6f-4879-befb-818879307a45";
        String lotteryId="3";
        String lotteryName="1";
        String value=bankUserSign+proof+time+lotteryId+lotteryName+key;
        System.out.println(value);
        String token = new String(Md5Util.md5(value).toUpperCase());
        JSONObject data=new JSONObject();
        data.put("bank_user_sign",bankUserSign);
        data.put("proof",proof);
        data.put("time",time);
        data.put("lottery_id",lotteryId);
        data.put("lottery_name",lotteryName);
        data.put("token",token);
        System.out.println(data.toJSONString());
        doPost("https://csp-brdc.dccnet.com.cn/weChatActivity/activity/updatePrizeStatus",data);
    }*/

    public static void main(String[] args) throws Exception {
        String openid = "oHLDewHNXVvLDlLg6yPUcfDx6EiM";
        String url = "https://api.mch.weixin.qq.com/v3/marketing/favor/users/" + openid + "/coupons";
        JSONObject data = new JSONObject();
        data.put("stock_id", "15419333");//代金券批次id也就是商品编号
        data.put("out_request_no", "111111");//订单号
        data.put("appid", "wxff7a18f70da090cd");
        data.put("stock_creator_mchid", "1605617216");//商户号
        SignUtil signUtil = new SignUtil();
        JSONObject jsonObject = doPost(url, data, signUtil.getToken("POST", HttpUrl.parse(url), data.toJSONString()));
    }

   /* public static void main(String[] args) {
        String token = new String(Md5Utils.md5("oHLDewHNXVvLDlLg6yPUcfDx6EiM.1611288053287.9.LD166T"));
    }*/


}
